#Boa:Dialog:SettingDialog

import wx
import MainFrame

def create(parent):
    return SettingDialog(parent)

[wxID_SETTINGDIALOG, wxID_SETTINGDIALOGCANCELBUTTON, wxID_SETTINGDIALOGEMPTY, 
 wxID_SETTINGDIALOGFETCHSERVERTEXT, wxID_SETTINGDIALOGIPV6ENABLEBOX, 
 wxID_SETTINGDIALOGIPV6ENABLETEXT, wxID_SETTINGDIALOGIPV6PRIORITYBOX, 
 wxID_SETTINGDIALOGIPV6PRIORITYTEXT, wxID_SETTINGDIALOGSAVEBUTTON, 
 wxID_SETTINGDIALOGTEXTCTRL1, 
] = [wx.NewId() for _init_ctrls in range(10)]

class SettingDialog(wx.Dialog):
    def _init_coll_flexGridSizer1_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.FetchServerText, 0, border=5,
              flag=wx.EXPAND | wx.LEFT | wx.TOP)
        parent.AddWindow(self.textCtrl1, 1, border=5,
              flag=wx.EXPAND | wx.LEFT | wx.TOP)
        parent.AddWindow(self.Ipv6EnableText, 0, border=5,
              flag=wx.LEFT | wx.TOP)
        parent.AddWindow(self.Ipv6EnableBox, 0, border=5, flag=wx.LEFT | wx.TOP)
        parent.AddWindow(self.Ipv6PriorityText, 0, border=5,
              flag=wx.BOTTOM | wx.LEFT | wx.TOP)
        parent.AddWindow(self.Ipv6PriorityBox, 0, border=5,
              flag=wx.BOTTOM | wx.LEFT | wx.TOP)

    def _init_coll_boxSizer1_Items(self, parent):
        # generated method, don't edit

        parent.AddSizer(self.flexGridSizer1, 1, border=0, flag=wx.EXPAND)
        parent.AddSizer(self.boxSizer2, 0, border=0, flag=wx.EXPAND)

    def _init_coll_boxSizer2_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.Empty, 1, border=0, flag=wx.EXPAND)
        parent.AddWindow(self.SaveButton, 0, border=5,
              flag=wx.BOTTOM | wx.LEFT | wx.TOP)
        parent.AddWindow(self.cancelbutton, 0, border=5,
              flag=wx.BOTTOM | wx.TOP | wx.RIGHT)

    def _init_sizers(self):
        # generated method, don't edit
        self.flexGridSizer1 = wx.FlexGridSizer(cols=2, hgap=0, rows=0, vgap=0)

        self.boxSizer1 = wx.BoxSizer(orient=wx.VERTICAL)

        self.boxSizer2 = wx.BoxSizer(orient=wx.HORIZONTAL)

        self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)
        self._init_coll_boxSizer1_Items(self.boxSizer1)
        self._init_coll_boxSizer2_Items(self.boxSizer2)

        self.SetSizer(self.boxSizer1)

    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_SETTINGDIALOG, name='SettingDialog',
              parent=prnt, pos=wx.Point(462, 415), size=wx.Size(358, 144),
              style=wx.DEFAULT_DIALOG_STYLE, title='Setting')
        self.SetClientSize(wx.Size(342, 108))

        self.FetchServerText = wx.StaticText(id=wxID_SETTINGDIALOGFETCHSERVERTEXT,
              label='Fetch Server:', name='FetchServerText', parent=self,
              pos=wx.Point(5, 5), size=wx.Size(75, 22), style=0)

        self.textCtrl1 = wx.TextCtrl(id=wxID_SETTINGDIALOGTEXTCTRL1,
              name='textCtrl1', parent=self, pos=wx.Point(85, 5),
              size=wx.Size(251, 22), style=0, value='')

        self.Ipv6EnableText = wx.StaticText(id=wxID_SETTINGDIALOGIPV6ENABLETEXT,
              label='Ipv6 Enable', name='Ipv6EnableText', parent=self,
              pos=wx.Point(5, 32), size=wx.Size(75, 14), style=0)

        self.Ipv6EnableBox = wx.CheckBox(id=wxID_SETTINGDIALOGIPV6ENABLEBOX,
              label='', name='Ipv6EnableBox', parent=self, pos=wx.Point(85, 32),
              size=wx.Size(79, 14), style=0)
        self.Ipv6EnableBox.SetValue(False)

        self.Ipv6PriorityText = wx.StaticText(id=wxID_SETTINGDIALOGIPV6PRIORITYTEXT,
              label='Ipv6 Priority', name='Ipv6PriorityText', parent=self,
              pos=wx.Point(5, 51), size=wx.Size(65, 14), style=0)

        self.Ipv6PriorityBox = wx.CheckBox(id=wxID_SETTINGDIALOGIPV6PRIORITYBOX,
              label='', name='Ipv6PriorityBox', parent=self, pos=wx.Point(85,
              51), size=wx.Size(79, 14), style=0)
        self.Ipv6PriorityBox.SetValue(True)

        self.SaveButton = wx.Button(id=wxID_SETTINGDIALOGSAVEBUTTON,
              label='Save', name='SaveButton', parent=self, pos=wx.Point(187,
              79), size=wx.Size(75, 24), style=0)
        self.SaveButton.Bind(wx.EVT_BUTTON, self.OnSaveButtonButton,
              id=wxID_SETTINGDIALOGSAVEBUTTON)

        self.cancelbutton = wx.Button(id=wxID_SETTINGDIALOGCANCELBUTTON,
              label='Cancel', name='cancelbutton', parent=self,
              pos=wx.Point(262, 79), size=wx.Size(75, 24), style=0)
        self.cancelbutton.Bind(wx.EVT_BUTTON, self.OnCancelbuttonButton,
              id=wxID_SETTINGDIALOGCANCELBUTTON)

        self.Empty = wx.StaticText(id=wxID_SETTINGDIALOGEMPTY, label='',
              name='Empty', parent=self, pos=wx.Point(0, 74), size=wx.Size(182,
              34), style=0)

        self._init_sizers()

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.setFetchServer(parent)
        self.parent = parent
        
    def setFetchServer(self,parent):
        server = parent.FetchServerValueText.GetLabel()
        if server.startswith("https://"):
            pos = 8
        else:
            pos = 7
        self.textCtrl1.ChangeValue(server[pos::])
            

    def OnSaveButtonButton(self, event):
        if self.Ipv6PriorityBox.GetValue():
            Priority = 6
        else:
            Priority = 4
        variable_dict = {
            "FETCH_SERVER":self.textCtrl1.GetValue(),
            "IPV6_ENABLE":self.Ipv6EnableBox.GetValue(),
            "PROTOCOL":Priority
        }
        self.parent.presenter.save(variable_dict)
        self.Close()

    def OnCancelbuttonButton(self, event):
        self.Close()


if __name__ == '__main__':
    app = wx.PySimpleApp()
    dlg = create(None)
    try:
        dlg.ShowModal()
    finally:
        dlg.Destroy()
    app.MainLoop()
